package job

import (
	"log/slog"
	"os"

	"GoMaestro/internal/conf"

	"github.com/robfig/cron/v3"
)

var jobMap = make(map[string]func())

func Init() {
	c := cron.New()

	for name, job := range conf.C.Job {
		_, err := c.AddFunc(job.Cron, jobMap[name])
		if err != nil {
			slog.Error("AddFunc error", slog.Any("error", err))
			os.Exit(5)
		}
	}
	c.Start()
}

func Register(name string, fn func()) {
	jobMap[name] = fn
}
